package com.example.renyang.happysell;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.renyang.happysell.MyUntil.ImageUtils;
import com.example.renyang.happysell.MyUntil.PhotoUtil;
import com.example.renyang.happysell.MyUntil.Tools;

public class EditUserActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText user_name;
    private ImageView user_face;

    private String user_face_path;

    private PhotoUtil mPhotoUtil;
    private PhotoUtil.Config mFaceConfig;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_edit_user);

        user_name = (EditText) findViewById(R.id.user_name_ET);
        user_face = (ImageView) findViewById(R.id.iv_face);

        user_face.setOnClickListener(this);

        // 工具类上传图片的
        mPhotoUtil = new PhotoUtil(this);
        mFaceConfig = new PhotoUtil.Config().setIsCrop(true)
                .setIsReturnImage(true).setCropOption(new PhotoUtil.CropOption(1, 1, 480, 480))
                .setPhotoCallback(faceCallback);
    }

        PhotoUtil.PhotoCallback faceCallback=new PhotoUtil.PhotoCallback(){

            @Override
            public void onBack(final Bitmap bitmap, final String path) {
                Bitmap bitmap2= Tools.getFitBitmap(path, Tools.dip2px(getApplicationContext(), 61));
                user_face_path=path;
                Bitmap roundedCornerBitmap= ImageUtils.getRoundedCornerBitmap(bitmap2, Tools.dip2px(getApplicationContext(), 3));
                user_face.setImageBitmap(roundedCornerBitmap);
            }
        };



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_face:  //点击添加图片
                InputMethodManager mInputMethod=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                if(mInputMethod.isActive()){
                    mInputMethod.hideSoftInputFromInputMethod(getCurrentFocus().getApplicationWindowToken(),0);
                }
                mPhotoUtil.takePhoto(mFaceConfig);
                break;
            case R.id.edituser_commitBtn:  //点击提交按钮
//                if (TextUtils.isEmpty(user_name.getText().toString().trim())) {
//                    Toast.makeText(this, "昵称不能为空或全部为空格", Toast.LENGTH_SHORT).show();
//                    return;
//                }

                if(TextUtils.isEmpty(user_face_path)){
                    Toast.makeText(this, "请选择头像", Toast.LENGTH_SHORT).show();
                }
//                loadDataFromServer();
//                 uploadFile(actionUrl);
                break;

            default: break;
        }
    }
}
